angular.module("app").controller("homeCtrl", ["$scope", "$http", "$state", "$filter", "treeViewDataSource", "orgList", "$timeout", function ($scope, $http, $state, $filter, treeViewDataSource, orgList, $timeout) {
    if (sessionStorage.userInfo) {
        $scope.userInfo = JSON.parse(sessionStorage.userInfo);
    }
    else {
        location.href = "#/login";
    }
    $scope.selectedItems = [];
    $scope.accordionDataSource = [
        {
            id: 1,
            title: "机构管理",
            menus: [{name: "机构列表", value: "home.org.info"}, 
                    {name: "机构维护", value: "home.org.list"}, 
                    {name: "管理员维护",value: "home.org.managerList"}
            ]
        },
        {
            id: 2,
            title: "社团管理",
            menus: [{name: "社团列表", value: "home.group.list"},{name: "挂靠审核",value: "home.group.verify"},{name: "未挂靠列表",value: "home.group.unlist"}]
        },
        {
            id: 3,
            title: "个人设置",
            menus: [{name: "修改密码", value: "home.user.changePassword"},{name: "退出",value: ""}]
        }
    ];
    // if($scope.userInfo.role == 2){
    //     $scope.accordionDataSource.push({
    //         id:3,
    //         title:"系统设置",
    //         menus:[{name:"机构信息设置",value:"home.setting"}]
    //     })
    // }
    $scope.accordionOptions = {
        dataSource: $scope.accordionDataSource,
        animationDuration: 300,
        collapsible: true,
        bindingOptions: {
            selectedItems: "selectedItems"
        }
    };

    $scope.$on("$stateChangeSuccess", function (event, toState, toParams, fromState, fromParams) {
        if (toState.name.indexOf("org") > 0) {
            $scope.selectedItems = [$scope.accordionDataSource[0]];
        }
        else if (toState.name.indexOf("group") > 0) {
            $scope.selectedItems = [$scope.accordionDataSource[1]];
        }
        else if(toState.name.indexOf("user") > 0){
            $scope.selectedItems = [$scope.accordionDataSource[2]];
        }
    });

    $scope.logout = function () {
        if (confirm("确定要退出吗?")) {
            sessionStorage.clear();
            location.href = "#/login";
        }
    };
    $scope.eventStop = function ($event) {
        $event.stopPropagation();
    };
    $scope.goBack = function () {
        history.back();
    };
    $scope.transformTreeData = function (data) {
        var arr = [];
        var obj = {};
        data.forEach(function (item) {
            item.href = item.orgId;
            item.id = item.orgId;
            item.text = item.orgName;
            item.expanded = true;
            obj[item.orgId] = item;
            if (item.orgId == $scope.userInfo.orgId) {
                arr.push(item);
            }
            else if (item.orgParent) {
                if (!obj[item.orgParent].items) {
                    obj[item.orgParent].items = [];
                }
                item.expanded = false;
                obj[item.orgParent].items.push(item);
            }
        });
        return arr;
    };
    $scope.city = city;
    $scope.District = District;
    $scope.province = province;

    //District.forEach(function(item){
    //    $scope.District[item.Id] = item;
    //});
    //city.forEach(function(item){
    //    $scope.city[item.CityID] = item;
    //});
    //province.forEach(function(item){
    //    $scope.province[item.ProID] = item;
    //});

    $scope.treeViewConfig = {
        bindingOptions: {
            dataSource: "treeViewConfig.dataSource"
        },
        expandAllEnabled: true
    };
    $scope.treeViewDataSource = $scope.transformTreeData(treeViewDataSource.data);
    $scope.treeViewDataSource[0].selected = true;
    $scope.treeViewConfig.dataSource = $scope.treeViewDataSource;
    $scope.orgList = {};
    orgList.data.forEach(function (item) {
        $scope.orgList[item.orgId] = item;
    });
    
    $scope.verifyTabs = {"0":true,"2":false,"-1":false};
}]);